import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import store from "../store";
import Layout from "@/views/layout/layout.vue";
const login = () => import("@/views/login/login.vue");
const Home = () => import("@/views/Home.vue");
const Variable = () => import("@/views/baseLearn/variable.vue");
const Pass53 = () => import("@/views/fnirs/pass53.vue");
const Websocket = () => import("@/views/tools/websocket.vue");

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    component: Layout,
    name: "dashboard",
    redirect: "/dashboard",
    meta: {
      isShow: true,
      title: "首页",
      icon: "el-icon-s-promotion"
    },
    children: [
      {
        path: "dashboard",
        name: "dashboard",
        component: Home,
        meta: {
          isShow: true,
          title: "首页",
          icon: "el-icon-s-promotion"
        }
      }
    ]
  },
  {
    path: "/base",
    component: Layout,
    name: "base",
    redirect: "/about",
    meta: {
      isShow: true,
      title: "基础学习",
      icon: "el-icon-s-promotion"
    },
    children: [
      {
        path: "/about",
        name: "About",
        component: () => import("../views/About.vue"),
        meta: {
          isShow: true,
          title: "关于",
          icon: "el-icon-s-promotion"
        }
      },
      {
        path: "/variable",
        name: "variable",
        component: Variable,
        meta: {
          isShow: true,
          title: "变量",
          icon: "el-icon-s-promotion"
        }
      }
    ]
  },
  {
    path: "/fnirs",
    component: Layout,
    name: "fnirs",
    redirect: "/pass53",
    meta: {
      isShow: true,
      title: "近红外",
      icon: "el-icon-s-promotion"
    },
    children: [
      {
        path: "/pass53",
        name: "pass53",
        component: Pass53,
        meta: {
          isShow: true,
          title: "53导图",
          icon: "el-icon-s-promotion"
        }
      }
    ]
  },
  {
    path: "/tools",
    component: Layout,
    name: "tools",
    redirect: "/websocket",
    meta: {
      isShow: true,
      title: "工具集",
      icon: "el-icon-s-promotion"
    },
    children: [
      {
        path: "/websocket",
        name: "websocket",
        component: Websocket,
        meta: {
          isShow: true,
          title: "Websocket测试",
          icon: "el-icon-s-promotion"
        }
      }
    ]
  },

  {
    path: "/login",
    name: "Login",
    component: login,
    meta: {
      isShow: false,
      title: "登录",
      icon: "el-icon-s-promotion"
    }
  }
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});

// to, from,
router.beforeEach((to, from, next) => {
  const token = store.getters.token;
  const require = true;
  if (require) {
    if (token) {
      if (to.path == "/login") {
        next({ path: "/login" });
      } else {
        next();
      }
    } else {
      if (to.path == "/login") {
        next();
      } else {
        next({
          path: "/login", // 未登录则跳转至login页面
          query: { redirect: to.fullPath } // 登陆成功后回到当前页面，这里传值给login页面，to.fullPath为当前点击的页面
        });
      }
    }
  } else {
    next();
  }
});
// to, from
router.afterEach(() => {});

export default router;
